package com.fenda.myviewpager

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.fenda.myviewpager.databinding.ActivityMainBinding
import com.google.android.material.tabs.TabLayoutMediator

class   MainActivity : AppCompatActivity() {
    private var _binding:ActivityMainBinding? = null
    private val binding  get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        _binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)

        binding.viewpager2.adapter = object :FragmentStateAdapter(this){
            override fun getItemCount():Int  = 3

            override fun createFragment(position: Int): Fragment {
               return when(position) {
                   0 -> ScaleFragment()
                   1 -> RotateFragment()
                   else -> TranslateFragment()
               }
            }
        }

        TabLayoutMediator(binding.tab,binding.viewpager2) { tab, position ->
            when (position) {
                0 -> tab.text = "缩放"
                1 -> tab.text = "旋转"
                else -> tab.text = "移动"
            }
        }.attach()
    }
}